4
תגובות
כשאני מסה לשלוח נתונים בין שני forms אז הנתונים מגיעים אבל אם אני קובע לשנות משהו בתוך הform שהוא לא הform שממנו נשלחים הנתונים אז כלום לא משתנה.
איך אפשר לתקן את זה?
איך אפשר לתקן את זה?
4 תשובות
זה ב2 דברים:
ב1(החלפה):
הטופס ששולח:
private void button1_Click(object sender, EventArgs e)
{
Form1 a = new Form1();
a.changetext(textBox1.Text, textBox2.Text);
}
{
Form1 a = new Form1();
a.changetext(textBox1.Text, textBox2.Text);
}
הטופס שמקבל:
public void changetext(string a, string b)
{
this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(a, b);
}
{
this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(a, b);
}
ב2(חיפוש):
הטופס ששולח:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ct(textBox1.Text);
}
{
Form1 form = new Form1();
form.ct(textBox1.Text);
}
הטופס שמקבל:
public void ct(string a)
{
richTextBox1.BackColor = Color.White;
int len = this.richTextBox1.TextLength;
int index = 0;
string temp = richTextBox1.Text;
richTextBox1.Text = "";
richTextBox1.Text = temp;
int lastIndex = this.richTextBox1.Text.LastIndexOf(a);
while (index < lastIndex)
{
this.richTextBox1.Find(a, index, len, RichTextBoxFinds.None);
this.richTextBox1.SelectionBackColor = Color.Yellow;
index = this.richTextBox1.Text.IndexOf(a, index) + 1;
}
}
{
richTextBox1.BackColor = Color.White;
int len = this.richTextBox1.TextLength;
int index = 0;
string temp = richTextBox1.Text;
richTextBox1.Text = "";
richTextBox1.Text = temp;
int lastIndex = this.richTextBox1.Text.LastIndexOf(a);
while (index < lastIndex)
{
this.richTextBox1.Find(a, index, len, RichTextBoxFinds.None);
this.richTextBox1.SelectionBackColor = Color.Yellow;
index = this.richTextBox1.Text.IndexOf(a, index) + 1;
}
}
עכשיו הבעיה היא שהקודים הללו עובדים אם הם נשלחים מאותו הטופס, הנתונים נשלחים לטופס השני ורק זה לא משנה כלום בטופס שמקבל.
הוא ארוך אבל:
public Form1()
{
InitializeComponent();
}
private string ReadFile(string f)
{
StreamReader sr = new StreamReader(f);
try
{
return sr.ReadToEnd();
}
catch
{
return "";
}
finally
{
sr.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfi = new SaveFileDialog();
sfi.DefaultExt = "txt";
sfi.Filter = "Text Files *.txt|*.txt|HTML Files *.html|*.html|HTML Files *.htm|*.htm|PHP Files *.php|*.php|JavaScript Files *.js| *.js|CSS Files *.css|*.css|ASP Files *.asp|*.asp|ASP.NET Files *.aspx|*.aspx";
sfi.Title = "שמירת קובץ";
if (sfi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SaveFile(sfi.FileName);
if(textBox1.Text == "") {
textBox1.Text = sfi.FileName;
}
}
}
private void SaveFile(string p)
{
StreamWriter fd = new StreamWriter(p);
try
{
fd.Write(richTextBox1.Text);
}
catch
{
}
finally
{
fd.Close();
}
MessageBox.Show("הקובץ נשמר בהצלחה");
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
File.Delete(textBox1.Text);
MessageBox.Show("הקובץ נמחק בהצלחה");
}
else
{
MessageBox.Show("אפשר למחוק רק קובץ שפתחת");
}
richTextBox1.Text = "";
textBox1.Text = "";
richTextBox1.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
closeFileToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
saveToolStripMenuItem.Enabled = false;
changeFontToolStripMenuItem.Enabled = false;
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox3.TextLength > 0 && textBox4.TextLength > 0)
{
richTextBox1.Text = richTextBox1.Text.Replace(textBox3.Text, textBox4.Text);
textBox3.Text = "";
textBox4.Text = "";
}
else
{
}
}
private void button7_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && textBox1.Text != "")
{
SaveFile(textBox1.Text);
}
else
{
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ope = new OpenFileDialog();
ope.FileName = "";
ope.Filter = "Text Files *.txt|*.txt|HTML Files *.html|*.html|HTML Files *.htm|*.htm|PHP Files *.php|*.php|JavaScript *.js|*.js|CSS Files *.css|*.css|ASP Files *.asp|*.asp|ASP.NET Files *.aspx|*.aspx";
ope.Title = "פתיחת קובץ";
if (ope.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
changeFontToolStripMenuItem.Enabled = true;
saveFileToolStripMenuItem.Enabled = true;
createFileToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = true;
closeFileToolStripMenuItem.Enabled = true;
textBox1.Text = ope.InitialDirectory + ope.FileName;
richTextBox1.Enabled = true;
richTextBox1.Text = ReadFile(ope.FileName);
}
}
private void createFileToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFontToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = true;
richTextBox1.Enabled = true;
closeFileToolStripMenuItem.Enabled = true;
}
private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFontToolStripMenuItem.Enabled = false;
textBox1.Text = "";
richTextBox1.Text = "";
createFileToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
openFileToolStripMenuItem.Enabled = true;
closeFileToolStripMenuItem.Enabled = false;
richTextBox1.Enabled = false;
}
private void deleteFileToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFontToolStripMenuItem.Enabled = false;
saveToolStripMenuItem.Enabled = false;
createFileToolStripMenuItem.Enabled = true;
openFileToolStripMenuItem.Enabled = true;
closeFileToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
if (textBox1.Text != "")
{
File.Delete(textBox1.Text);
MessageBox.Show("הקובץ נמחק בהצלחה");
}
else
{
MessageBox.Show("אפשר למחוק רק קובץ שפתחת");
}
richTextBox1.Text = "";
textBox1.Text = "";
richTextBox1.Enabled = false;
}
private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && textBox1.Text != "")
{
SaveFile(textBox1.Text);
}
else
{
}
}
private void saveFileAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfi = new SaveFileDialog();
sfi.DefaultExt = "txt";
sfi.Filter = "Text Files *.txt|*.txt|HTML Files *.html|*.html|HTML Files *.htm|*.htm|PHP Files *.php|*.php|JavaScript Files *.js| *.js|CSS Files *.css|*.css|ASP Files *.asp|*.asp|ASP.NET Files *.aspx|*.aspx";
sfi.Title = "שמירת קובץ";
if (sfi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SaveFile(sfi.FileName);
if (textBox1.Text == "")
{
textBox1.Text = sfi.FileName;
}
}
}
private void changeFontToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dialog = fontDialog1.ShowDialog();
if(dialog == DialogResult.OK)
{
Font font = fontDialog1.Font;
richTextBox1.SelectionFont = font;
}
else
{
}
}
public void changetext(string a, string b)
{
this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(a, b);
}
private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.ShowDialog();
}
private void changeColorToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionColor = color.Color;
}
else
{
}
}
public void ct(string a)
{
richTextBox1.BackColor = Color.White;
int len = this.richTextBox1.TextLength;
int index = 0;
string temp = richTextBox1.Text;
richTextBox1.Text = "";
richTextBox1.Text = temp;
int lastIndex = this.richTextBox1.Text.LastIndexOf(a);
while (index < lastIndex)
{
this.richTextBox1.Find(a, index, len, RichTextBoxFinds.None);
this.richTextBox1.SelectionBackColor = Color.Yellow;
index = this.richTextBox1.Text.IndexOf(a, index) + 1;
}
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 form = new Form4();
form.ShowDialog();
}
{
InitializeComponent();
}
private string ReadFile(string f)
{
StreamReader sr = new StreamReader(f);
try
{
return sr.ReadToEnd();
}
catch
{
return "";
}
finally
{
sr.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfi = new SaveFileDialog();
sfi.DefaultExt = "txt";
sfi.Filter = "Text Files *.txt|*.txt|HTML Files *.html|*.html|HTML Files *.htm|*.htm|PHP Files *.php|*.php|JavaScript Files *.js| *.js|CSS Files *.css|*.css|ASP Files *.asp|*.asp|ASP.NET Files *.aspx|*.aspx";
sfi.Title = "שמירת קובץ";
if (sfi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SaveFile(sfi.FileName);
if(textBox1.Text == "") {
textBox1.Text = sfi.FileName;
}
}
}
private void SaveFile(string p)
{
StreamWriter fd = new StreamWriter(p);
try
{
fd.Write(richTextBox1.Text);
}
catch
{
}
finally
{
fd.Close();
}
MessageBox.Show("הקובץ נשמר בהצלחה");
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
File.Delete(textBox1.Text);
MessageBox.Show("הקובץ נמחק בהצלחה");
}
else
{
MessageBox.Show("אפשר למחוק רק קובץ שפתחת");
}
richTextBox1.Text = "";
textBox1.Text = "";
richTextBox1.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
closeFileToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
saveToolStripMenuItem.Enabled = false;
changeFontToolStripMenuItem.Enabled = false;
}
private void button6_Click(object sender, EventArgs e)
{
if (textBox3.TextLength > 0 && textBox4.TextLength > 0)
{
richTextBox1.Text = richTextBox1.Text.Replace(textBox3.Text, textBox4.Text);
textBox3.Text = "";
textBox4.Text = "";
}
else
{
}
}
private void button7_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && textBox1.Text != "")
{
SaveFile(textBox1.Text);
}
else
{
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ope = new OpenFileDialog();
ope.FileName = "";
ope.Filter = "Text Files *.txt|*.txt|HTML Files *.html|*.html|HTML Files *.htm|*.htm|PHP Files *.php|*.php|JavaScript *.js|*.js|CSS Files *.css|*.css|ASP Files *.asp|*.asp|ASP.NET Files *.aspx|*.aspx";
ope.Title = "פתיחת קובץ";
if (ope.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
changeFontToolStripMenuItem.Enabled = true;
saveFileToolStripMenuItem.Enabled = true;
createFileToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = true;
closeFileToolStripMenuItem.Enabled = true;
textBox1.Text = ope.InitialDirectory + ope.FileName;
richTextBox1.Enabled = true;
richTextBox1.Text = ReadFile(ope.FileName);
}
}
private void createFileToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFontToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = true;
richTextBox1.Enabled = true;
closeFileToolStripMenuItem.Enabled = true;
}
private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFontToolStripMenuItem.Enabled = false;
textBox1.Text = "";
richTextBox1.Text = "";
createFileToolStripMenuItem.Enabled = true;
saveToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
openFileToolStripMenuItem.Enabled = true;
closeFileToolStripMenuItem.Enabled = false;
richTextBox1.Enabled = false;
}
private void deleteFileToolStripMenuItem_Click(object sender, EventArgs e)
{
changeFontToolStripMenuItem.Enabled = false;
saveToolStripMenuItem.Enabled = false;
createFileToolStripMenuItem.Enabled = true;
openFileToolStripMenuItem.Enabled = true;
closeFileToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
if (textBox1.Text != "")
{
File.Delete(textBox1.Text);
MessageBox.Show("הקובץ נמחק בהצלחה");
}
else
{
MessageBox.Show("אפשר למחוק רק קובץ שפתחת");
}
richTextBox1.Text = "";
textBox1.Text = "";
richTextBox1.Enabled = false;
}
private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text) && textBox1.Text != "")
{
SaveFile(textBox1.Text);
}
else
{
}
}
private void saveFileAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfi = new SaveFileDialog();
sfi.DefaultExt = "txt";
sfi.Filter = "Text Files *.txt|*.txt|HTML Files *.html|*.html|HTML Files *.htm|*.htm|PHP Files *.php|*.php|JavaScript Files *.js| *.js|CSS Files *.css|*.css|ASP Files *.asp|*.asp|ASP.NET Files *.aspx|*.aspx";
sfi.Title = "שמירת קובץ";
if (sfi.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
SaveFile(sfi.FileName);
if (textBox1.Text == "")
{
textBox1.Text = sfi.FileName;
}
}
}
private void changeFontToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dialog = fontDialog1.ShowDialog();
if(dialog == DialogResult.OK)
{
Font font = fontDialog1.Font;
richTextBox1.SelectionFont = font;
}
else
{
}
}
public void changetext(string a, string b)
{
this.richTextBox1.Rtf = this.richTextBox1.Rtf.Replace(a, b);
}
private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.ShowDialog();
}
private void changeColorToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
richTextBox1.SelectionColor = color.Color;
}
else
{
}
}
public void ct(string a)
{
richTextBox1.BackColor = Color.White;
int len = this.richTextBox1.TextLength;
int index = 0;
string temp = richTextBox1.Text;
richTextBox1.Text = "";
richTextBox1.Text = temp;
int lastIndex = this.richTextBox1.Text.LastIndexOf(a);
while (index < lastIndex)
{
this.richTextBox1.Find(a, index, len, RichTextBoxFinds.None);
this.richTextBox1.SelectionBackColor = Color.Yellow;
index = this.richTextBox1.Text.IndexOf(a, index) + 1;
}
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 form = new Form4();
form.ShowDialog();
}